﻿using System;
using System.Collections.Generic;
using System.Text;
using FeedDotNet.Common;
using System.Collections;

namespace ConsoleApplication
{
    class MovieFeedItem : FeedItem
    {
        public string year { get; set; }
        public string resolution { get; set; }
        public string plot { get; set; }
        public string imageURL { get; set; }
        public string nzb { get; set; }
        public string rating { get; set; }
        public string genre { get; set; }
        public string feedTitle { get; set; }
        public string imdbID { get; set; }
        public string runTime { get; set; }
        public string director { get; set; }
        public ArrayList actors { get; set; }
        public string mpaa { get; set; }

        public override FeedGuid Guid
        {
            get
            {
                return new FeedGuid(string.Format("{0}::{1}", 
                    string.IsNullOrEmpty(this.imdbID)?this.Title:this.imdbID, 
                    this.resolution));
            }
            set
            {
                throw new ApplicationException("you can't set this... shut up");
            }
        }

        public MovieFeedItem(FeedItem item)
            : base(item)
        {
            this.year = "";
            this.resolution = "";
            this.plot = "";
            this.imageURL = "";
            this.nzb = "";
            this.rating = "";
            this.genre = "";
            this.feedTitle = item.Title;
            this.imdbID = "";
            this.actors = new ArrayList();
            this.director = "";
            this.mpaa = "";

        }

        public static int DateComparer(FeedItem item, FeedItem item2)
        {
            if (!item.Published.HasValue)
                return 1;
            else if (!item2.Published.HasValue)
                return -1;
            else
                return item2.Published.Value.CompareTo(item.Published.Value);
        }

        private string cachedCacheString;
        public string GetCacheCompareString()
        {
            if (cachedCacheString == null)
            {
                cachedCacheString = this.guid;
            }

            return cachedCacheString;
        }

        public static List<FeedItem> Except(List<FeedItem> oldList, List<FeedItem> newList)
        {
            List<FeedItem> newFeedItems = new List<FeedItem>();
            Dictionary<string, bool> oldLookup = new Dictionary<string, bool>();

            foreach (FeedItem item in oldList)
            {
                if (item.Guid != null)
                    oldLookup[item.Guid] = true;
                else
                    oldLookup[item.Title] = true;
            }

            foreach (FeedItem item in newList)
            {
                MovieFeedItem mfi = item as MovieFeedItem;
                if (mfi == null)
                    continue;

                if (!oldLookup.ContainsKey(mfi.Guid))
                    newFeedItems.Add(item);
            }

            return newFeedItems;
        }

        public static List<MovieFeedItem> Distinct(List<MovieFeedItem> list)
        {
            Dictionary<string, MovieFeedItem> lookup = new Dictionary<string, MovieFeedItem>();

            foreach (MovieFeedItem item in list)
            {
                if (item.Guid != null)
                    lookup[item.Guid] = item;
                else
                    lookup[item.Title] = item;
            }

            return new List<MovieFeedItem>(lookup.Values);
        }

        public static List<FeedItem> Distinct(List<FeedItem> list)
        {
            Dictionary<string, FeedItem> lookup = new Dictionary<string, FeedItem>();

            foreach (FeedItem item in list)
            {
                if (item.Guid != null)
                    lookup[item.Guid] = item;
                else
                    lookup[item.Title] = item;
            }

            return new List<FeedItem>(lookup.Values);
        }

        public static List<FeedItem> Union(List<FeedItem> original, List<MovieFeedItem> newItems)
        {
            Dictionary<string, FeedItem> lookup = new Dictionary<string, FeedItem>();
            List<FeedItem> newByNameDate = new List<FeedItem>(original);

            foreach (FeedItem item in original)
            {
                if (item.Guid != null)
                    lookup[item.Guid] = item;
            }

            foreach (FeedItem item in newItems)
            {
                if (item.Guid != null && !lookup.ContainsKey(item.Guid))
                    newByNameDate.Add(item);
                else if (item.Guid == null)
                    newByNameDate.Add(item);
            }

            newByNameDate.Sort(DateComparer);

            return newByNameDate;
        }
    }
}
